/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package triseries;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import menuitem.BetaseriesLink;
import menuitem.JMenuPlugins;
import menuitem.MarquerVu;
import menuitem.MegaUpLink;
import menuitem.OpenDir;
import menuitem.StreamingLink;

/**
 * @author tshepo
 * 
 */
public class MyMenuItem extends JMenuItem {

	private static final boolean DEBUG = (Main.debug || MyProperties.debug
			.equalsIgnoreCase("MyMenuItem"));
	private static final long serialVersionUID = -2087915664878195291L;
	private String fileLink;
	
	public MyMenuItem(UnwatchEpisode uwe) {
		if (DEBUG)
			System.err.println("MyMenuItem("+uwe.GetShow()+uwe.GetSeason()+uwe.GetEpisode()+ ")");
		
		
		final JPopupMenu jpopup = new JPopupMenu();
		
		fileLink = VideoFile.makeName(uwe.GetShow(), uwe.getSeason(), uwe.getEpisode(), uwe.GetTitle());
		this.setText(fileLink.substring(fileLink.lastIndexOf(File.separator) + 1, fileLink.length()));

		jpopup.add(new MarquerVu(uwe));
		jpopup.add(new BetaseriesLink(uwe));
		JMenuPlugins mp = new StreamingLink(uwe);
		if(mp.isDisplay()) jpopup.add(mp);
		mp = new MegaUpLink(uwe);
		if(mp.isDisplay()) jpopup.add(mp);
		jpopup.add(new OpenDir(uwe));
		
		
		
		
		this.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				maybeShowPopup(e);
			}

			private void maybeShowPopup(MouseEvent e) {
				if (!(e.getButton() == MouseEvent.BUTTON1)) {
					jpopup.show(e.getComponent(), e.getX(), e.getY());

				} else {
					int i = 0;
					String openFile;
					do {
						if (MyProperties.nomRepDest[i].endsWith(File.separator))
							openFile = MyProperties.nomRepDest[i++] + fileLink;
						else
							openFile = MyProperties.nomRepDest[i++]
									+ File.separator + fileLink;
					} while (!FileAcces.openFile(openFile, MyProperties.exts
							.split(","))
							&& i < MyProperties.nomRepDest.length);
				}
			}
		});
		
	}
	
}
